﻿using IOC.Proxy;
using ORM.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ORM.Base
{
    public class DBFactory
    {
        public static Command CreateCommand(bool IsTrace)
        {
            //Console.WriteLine("Command生成初始化线程ID:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Command command = (Command)DynamictProxy.CreateProxyObject(typeof(Command), typeof(Command), typeof(CommandIntercept), true, false);
            command.TraceSQL = IsTrace;
            command.OptionID = ConfigUtil.GetUUID();
            //在Command的工厂生产者中判断事务管理的字典中是否包含当前线程的ID，如果有说明这个command参与了事务操作，对command对应的Transition赋值
            string TaskID = Thread.CurrentThread.ManagedThreadId.ToString();
            if (LocalTransactionManage.ContainsTaskID(TaskID))
            {
                command.Transition = LocalTransactionManage.GetTransitionTask(TaskID).Transition;
            }
            return command;
        }

        public static Record CreateRecord(bool IsTrace)
        {
            Record record = (Record)DynamictProxy.CreateProxyObject(typeof(Record), typeof(Record), typeof(RecordIntercept), true, false, false);
            record.TraceSQL = IsTrace;
            record.OptionID = ConfigUtil.GetUUID();
            return record;
        }


    }
}
